﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;
using Google.GData.Client;

namespace GJReminder.Utils
{
    public class ToolSet
    {
        public static SecurityContext GetSecurityContext()
        {
            string alterUrl = GJReminder.Properties.Settings.Default.AlterURL; //"http://www.google.com/calendar/feeds/ti07tqgpe5rsr2c7lqrc4hmp34%40group.calendar.google.com/private/full";
            string userName = GJReminder.Properties.Settings.Default.UserName;
            string passWord = GJReminder.Properties.Settings.Default.Password;

            if (string.IsNullOrEmpty(alterUrl))
            {
                alterUrl = "http://www.google.com/calendar/feeds/default/private/full";
            }

            CalendarService service = new CalendarService(GJReminder.Properties.Resources.AppName);
            service.setUserCredentials(userName, passWord);

            return new SecurityContext()
            {
                UserName = userName,
                Password = passWord,
                AlterUrl = alterUrl,
                Service = new ServiceWrapper(service, alterUrl)
            };
        }
    }

    public class SecurityContext
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string AlterUrl { get; set; }
        public ServiceWrapper Service { get; set; }
    }

    public class ServiceWrapper
    {
        private CalendarService m_service;
        private Uri m_uri;

        public ServiceWrapper(CalendarService service, string alterUrl)
        {
            m_service = service;
            m_uri = new Uri(alterUrl);
        }

        public TEntry Insert<TEntry>(TEntry entry) where TEntry : AtomEntry
        {
            return m_service.Insert(m_uri, entry);
        }

        public List<EventEntry> Query(DateTime startTime, DateTime endTime)//EventQuery eventfeedQuery)
        {
            EventQuery eventfeedQuery = new EventQuery();

            eventfeedQuery.StartTime = startTime;
            eventfeedQuery.EndTime = endTime;
            eventfeedQuery.Uri = m_uri;

            List<EventEntry> entryList = new List<EventEntry>();

            EventFeed eventFeed = m_service.Query(eventfeedQuery) as EventFeed;

            // now populate the calendar
            while (eventFeed != null && eventFeed.Entries.Count > 0)
            {
                entryList.AddRange(eventFeed.Entries.Cast<EventEntry>());
                // just query the same query again.
                if (eventFeed.NextChunk != null)
                {
                    eventfeedQuery.Uri = new Uri(eventFeed.NextChunk);
                    eventFeed = m_service.Query(eventfeedQuery) as EventFeed;
                }
                else
                    eventFeed = null;
            }

            return entryList;
        }
    }
}
